<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>


<html>
<head>
    <title>添加品牌信息</title>
    <%@ include file="../../common/base.jsp" %>

    <script type="text/javascript">

        function addSubmit() {
            var brandId = $("input[name='brandId']:checked").val();
            if(brandId == null || brandId == undefined || brandId.length < 1){
                prompt.alert("请选择需要关联的的品牌！");
                return false
            }

            var maintenanceId = $("input[name='maintenanceId']:checked").val();
            if(maintenanceId == null || maintenanceId == undefined || maintenanceId.length < 1){
                prompt.alert("请选择需要关联的的保养类型！");
                return false
            }

            var carproductsId = $("input[name='carproductsId']:checked").val();
            if(carproductsId == null || carproductsId == undefined || carproductsId.length < 1){
                prompt.alert("请选择需要关联的的汽车产品！");
                return false
            }

            $.ajax({
                type: "POST",
                url: "${pageContext.request.contextPath}/relationInfo/saveRelationinfo",
                dataType: "json",
                data: getParams(),
                success: function (data) {
                    if (data.errorCode == 0) {
                        prompt.alert("添加关联信息成功！");

                        setTimeout(function () {
                            window.location.href = "${pageContext.request.contextPath}/relationInfo/showRelationinfo";
                        }, 2000);
                    } else {
                        prompt.alert(data.msg);
                    }
                }
            });
        }

        //获取form表单所有参数
        function getParams() {
            var json = {};
            $(".inputInfoList3 input,.inputInfoList3 select").each(function (i, e) {
                json[$(e).attr("id")] = $(e).attr("value");
            });
            var brandId = $("input[name='brandId']:checked").val();
            json["brandId"] = brandId;

            var maintenanceId = $("input[name='maintenanceId']:checked").val();
            json["maintenanceId"] = maintenanceId;

            var carproductsId = $("input[name='carproductsId']:checked").val();
            json["carproductsId"] = carproductsId;

            return json;
        }

    </script>

</head>

<body id="body">
<!--head-->
<div id="head">
    <jsp:include page="../../common/head.jsp"></jsp:include>
</div>
<!--head end-->
<!--container-->
<div id="container">
    <div id="pageId" style="display: none;">05-06</div>
    <!--sidebar-->
    <%@ include file="../../common/menu.jsp" %>
    <!--sidebar-->
    <!--main-->
    <div class="main" id="mainDiv">
        <div class="channel_manage">
            <div class="location" style="background-position:22px -29px;">
                平台管理<span>&gt;</span>保养关联管理<span>&gt;</span>添加汽车保养关联信息
            </div>
            <form method="post" enctype="multipart/form-data">
                <!--content-->
                <div class="content">
                    <table style="width: 100%;height: 300px">
                        <tr>
                            <td style="width: 33%;">
                                <div class="listBox">
                                    <h3>汽车品牌列表</h3>
                                    <ul id="brandTree">
                                    </ul>
                                </div>
                            </td>
                            <td style="width: 33%;">
                                <div class="listBox" >
                                    <h3>汽车养列表</h3>
                                    <ul id="maintenanceTree">
                                    </ul>
                                </div>
                            </td>
                            <td style="width: 34%;">
                                <div class="listBox">
                                    <h3>汽车产品列表</h3>
                                    <ul id="carProductsTree">
                                    </ul>
                                </div>
                            </td>
                        </tr>
                    </table>
                    <div class="inputInfoList3">
                        <ul class="recharge_list">

                            <li>
                                <label>关联里程</label>
                                <select id="mileageId" name="mileageId" style="width: 280px;">
                                    <option value="">选择里程</option>
                                    <c:forEach var="item" items="${mileageList}" varStatus="vatS">
                                        <option value="${item.id}">${item.minMileage}-${item.maxMileage}</option>
                                    </c:forEach>
                                </select>
                            </li>
                            <li>
                                <label>上路时间</label>
                                <input type="text" id="roadTime" class="Wdate"
                                       onFocus="WdatePicker({dateFmt:'yyyy-MM',minDate:'1990-01'});this.style.border = '1px solid #2A7CCF'"
                                       onBlur="this.style.border = '1px solid #D5D5D5'"
                                       value="${roadTimeDate}"
                                       readonly/>
                            </li>
                            <li>
                                <label>是否推荐</label>
                                <select id="recommendSign" name="recommendSign" style="width: 280px;">
                                    <option value="0">否</option>
                                    <option value="1">是</option>
                                </select>
                            </li>
                        </ul>
                        <div class="button_wrap">
                            <a href="javascript:void(0);" onClick="addSubmit();" class="bg_green">完成</a>
                            <input type="reset" value="重置">
                        </div>
                    </div>
                </div>
                <!--content-->
            </form>
            <!--foot-->
            <%@ include file="../../common/foot.jsp" %>
            <!--foot-->
        </div>
    </div>
    <!--main-->
</div>
<script src="${pageContext.request.contextPath}/static/js/basic.js"></script>

<script type="text/javascript">
    $(document).ready(function(e) {
        $("#brandTree li").attr("id","0");
        $("#maintenanceTree li").attr("id","0");
        $("#carProductsTree li").attr("id","0");
        init();
    });
    //初始化加载渠道列表、权限树相关数据
    function init(parentId){
        //$("#content").showLoading();
        $.ajax({
            type: "POST",
            url: "${pageContext.request.contextPath}/brand/queryBrandTree",
            dataType:"json",
            data:{
                parentId:parentId
            },
            success: function(data){
                var rightData = data.rightData;

                //绑定权限树数据
                for (var j = 0; j < rightData.length; j++) {
                    createBrandTree(rightData[j]);
                }
            }
        });

        $.ajax({
            type: "POST",
            url: "${pageContext.request.contextPath}/carProducts/queryCarproductsTree",
            dataType:"json",
            data:{
                parentId:parentId
            },
            success: function(data){
                var rightData = data.rightData;

                //绑定权限树数据
                for (var j = 0; j < rightData.length; j++) {
                    createCarProductsTree(rightData[j]);
                }
            }
        });

        $.ajax({
            type: "POST",
            url: "${pageContext.request.contextPath}/maintenance/queryMaintenanceTree",
            dataType:"json",
            data:{
                parentId:parentId
            },
            success: function(data){
                var rightData = data.rightData;

                //绑定权限树数据
                for (var j = 0; j < rightData.length; j++) {
                    createMaintenanceTree(rightData[j]);
                }
            }
        });

        //$("#content").hideLoading();
    }

    //创建权限树
    function createCarProductsTree(obj){
        var li = "<li id='cp"+obj.id+"' value="+obj.parentId+">";
        if(obj.type == 2){
            li = li+"<input name='carproductsId' type='radio' value='"+obj.id+"'>";
        }
        li = li+"<span style='cursor:pointer;' onclick='spread(\"cp"+obj.id+"\");'>"+obj.productsName+"</span></li>";

        if (obj.parentId==0) {//一级菜单
            $("#carProductsTree").append(li);
            $("#cp"+obj.id+" input").attr("class",obj.parentId);
        }else {
            $("#cp"+obj.parentId).append("<ul class='cp"+obj.parentId+"' style='display:none;'>"+li+"</ul>");//找父节点追加元素
        }

        if (obj.SUB_DATA!=undefined) {
            $("#cp"+obj.id+" span").before("<i class='triangle_l'/>");
            for (var i = 0; i < obj.SUB_DATA.length; i++){
                createCarProductsTree(obj.SUB_DATA[i]);
            }
        }

    }

    //创建权限树
    function createMaintenanceTree(obj){
        var li = "<li id='maintenance"+obj.id+"' value="+obj.parentId+">";
        if(obj.type == 2){
            li = li + "<input name='maintenanceId' type='radio' value='"+obj.id+"'>";
        }
        li = li + "<span style='cursor:pointer;' onclick='spread(\"maintenance"+obj.id+"\");'>"+obj.maintenanceName+"</span></li>";
        if (obj.parentId==0) {//一级菜单
            $("#maintenanceTree").append(li);
            $("#maintenance"+obj.id+" input").attr("class","maintenance"+obj.parentId);
        }else {
            $("#maintenance"+obj.parentId).append("<ul class='maintenance"+obj.parentId+"' style='display:none;'>"+li+"</ul>");//找父节点追加元素
        }

        if (obj.SUB_DATA!=undefined) {
            $("#maintenance"+obj.id+" span").before("<i class='triangle_l'/>");
            for (var i = 0; i < obj.SUB_DATA.length; i++){
                createMaintenanceTree(obj.SUB_DATA[i]);
            }
        }

    }


    //创建权限树
    function createBrandTree(obj){
        var li = "<li id='brand"+obj.id+"' value="+obj.parentId+">" ;
        if(obj.brandLevel == 6){
            li = li+"<input name='brandId' type='radio' value='"+obj.id+"'>";
        }
        li = li+"<span style='cursor:pointer;' onclick='spread(\"brand"+obj.id+"\");'>"+obj.brandName+"</span></li>";

        if (obj.parentId==0) {//一级菜单
            $("#brandTree").append(li);
            $("#brand"+obj.id+" input").attr("class",obj.parentId);
        }else {
            $("#brand"+obj.parentId).append("<ul class='brand"+obj.parentId+"' style='display:none;'>"+li+"</ul>");//找父节点追加元素
        }

        if (obj.SUB_DATA!=undefined) {
            $("#brand"+obj.id+" span").before("<i class='triangle_l'/>");
            for (var i = 0; i < obj.SUB_DATA.length; i++){
                createBrandTree(obj.SUB_DATA[i]);
            }
        }

    }

    //权限树单个展开收缩
    function spread(clsName){


        $("."+clsName).each(function(i,e){
            if ($(e).is(":hidden")) {
                $(e).show();
            } else {
                $(e).hide();
            }
        });
    }

</script>

</body>
</html>
